package com.krtech.wecard.module.ai.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aliyun.tea.TeaModel;
import com.aliyun.viapi20230117.models.GetAsyncJobResultResponse;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.module.ai.GetAsyncJobResult;
import com.krtech.wecard.module.ai.entity.mergeVideoByPicVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/asyncJob")
@Api(value = "异步任务管理",tags = "异步任务管理")
public class AsyncJobController {

    private static String accessKeyId = "LTAI5tAK8JHwnntxE5ERvhbT";
    private static String accessKeySecret = "Y0A67pO2DloUcZDaZGpJrFtriCsDsP";

    @GetMapping("/getJobByRequestId")
    @ApiOperation(value = "根据RequestId获得视频地址")
    public String getJobByRequestId(String RequestId){
        if (StrUtil.isEmpty(RequestId)){
            throw new BusinessException("必填参数不能不为空");
        }

        String videoUrl = this.getById(RequestId);

        if (StrUtil.isEmpty(videoUrl)){
            throw new BusinessException("找不到对应的视频地址");
        }

        return videoUrl;
    }

    public String getById(String RequestId){
        try {
            // 创建AccessKey ID和AccessKey Secret，请参考https://help.aliyun.com/document_detail/175144.html
            // 如果您使用的是RAM用户的AccessKey，还需要为子账号授予权限AliyunVIAPIFullAccess，请参考https://help.aliyun.com/document_detail/145025.html
            // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
            com.aliyun.viapi20230117.Client client = GetAsyncJobResult.createClient(accessKeyId, accessKeySecret);
            com.aliyun.viapi20230117.models.GetAsyncJobResultRequest getAsyncJobResultRequest = new com.aliyun.viapi20230117.models.GetAsyncJobResultRequest()
                    .setJobId(RequestId);
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            GetAsyncJobResultResponse getAsyncJobResultResponse = client.getAsyncJobResultWithOptions(getAsyncJobResultRequest, runtime);
            // 获取整体结果。
//            System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(getAsyncJobResultResponse)));
            // 获取单个字段。
//            System.out.println(getAsyncJobResultResponse.getBody());
            String videoUrl = JSONUtil.parseObj(JSONUtil.parseObj(JSONUtil.parseObj(JSONUtil.parseObj(getAsyncJobResultResponse).get("body")).get("data")).get("result")).get("videoUrl").toString();
            return videoUrl;
        } catch (com.aliyun.tea.TeaException teaException) {
            teaException.printStackTrace();
            // 获取整体报错信息。
            System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
            // 获取单个字段。
            System.out.println(teaException.getCode());
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
